/**
 * Created by Administrator on 2017/11/16 0016.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxCustVideoUserDocModel = function (obj) {
    YjxBaseModel.call(this,obj);
};

YjxCustVideoUserDocModel.prototype = new YjxBaseModel();

// 取得表名
YjxCustVideoUserDocModel.prototype.getTableName = function(){
    return 'yjx_cust_video_user_doc';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxCustVideoUserDocModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'doc_id='+this.doc_id;
};



/*
* 更新用户自定义视频作品信息
*
* @author 王俊
* @date 2018-07-04
*
* @param {Object} options
* @param {Object} options.userId
* @param {Object} options.uniDocId
* @param {Object} options.docString
* @param {Function} callback
*                       error
* */
YjxCustVideoUserDocModel.prototype.updateCustomVideoDocInfo = function (options, callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxCustVideoUserDocModel.js [updateCustomVideoDocInfo] getConErr: ' + getConErr);
            return callback(getConErr, null);
        }

        var sql =
            ' UPDATE yjx.yjx_cust_video_user_doc ycvud ' +
            ' SET ycvud.data = ' + options.docString + ', ' +
            '     ycvud.res_low = NULL, ' +
            '     ycvud.comb_res_low = NULL, ' +
            '     ycvud.res_mid = NULL, ' +
            '     ycvud.comb_res_mid = NULL, ' +
            '     ycvud.res_high = NULL, ' +
            '     ycvud.comb_res_high = NULL, ' +
            '     ycvud.state = 0, ' +
            '     ycvud.dirty = 1, ' +
            '     ycvud.modify_cnt = ycvud.modify_cnt+1, ' +
            '     ycvud.update_time = now(), ' +
            '     ycvud.update_by = ' + options.userId +
            ' WHERE ycvud.uni_doc_id = ' + options.uniDocId +
            '   AND ycvud.user_id = ' + options.userId;
        console.log(sql);
        connection.query(sql, function (queryErr, result) {
            connection.release();
            if (queryErr) {
                console.log('YjxCustVideoUserDocModel.js [updateCustomVideoDocInfo] queryErr' + queryErr);
                return callback(queryErr, null);
            }
            return callback(queryErr, result);
        });
    })
};

/*
 * 修改自由创建的作品名
 *
 * 参数： userId   用户id
 * 参数： docId   作品统一id
 * 参数： name  作品名
 * 参数： callback   错误信息和结果
 */
YjxCustVideoUserDocModel.prototype.updateDocName = function(userId,docId,name, callback){
    pool.pool.getConnection(function(err, connection) {

        var sql = "	UPDATE	" +
            "	  `yjx`.`yjx_cust_video_user_doc`	" +
            "	SET	" +
            "	  `doc_name` = '"+name+"'	" +
            "	WHERE `uni_doc_id` = "+docId+"	" +
            "	AND user_id = "+userId +
            "   AND del_flg = 0 ";
        console.log(sql);
        connection.query(sql, function (error, result) {
            if (error) {
                console.log('[YjxCouponModel.js] [updateDocName]  Error: ' + error);
            }
            connection.release();

            callback(error, result);
        });
    });
};


/*
 * 获取自由创建的资源下载地址
 *
 * 参数： userId   用户id
 * 参数： docId   作品统一id
 * 参数： name  作品名
 * 参数： callback   错误信息和结果
 */
YjxCustVideoUserDocModel.prototype.getCustUserVideoDocDowUrl = function(userId,docId, callback){
    pool.pool.getConnection(function(err, connection) {

        var sql = "	SELECT	" +
                "	  yvr.url,	" +
                "	  yvr.file_size AS fileSize	" +
                "	FROM	" +
                "	  yjx.yjx_video_res yvr	" +
                "	WHERE yvr.res_id =	" +
                "	  (SELECT	" +
                "	    CASE	" +
                "	      WHEN cvud.res_high != 0	" +
                "	      THEN cvud.res_high	" +
                "	      WHEN cvud.res_mid != 0 " +
                "	      THEN cvud.res_mid	" +
                "	      ELSE cvud.res_low	" +
                "	    END AS res_id	" +
                "	  FROM	" +
                "	    yjx.yjx_cust_video_user_doc cvud	" +
                "	  WHERE cvud.del_flg = 0	" +
                "	    AND cvud.uni_doc_id = "+docId+" " +
                "       AND cvud.user_id = "+userId+" )"+
                "	    AND yvr.del_flg = 0	" ;
            console.log(sql);
        connection.query(sql, function (error, result) {
            if (error) {
                console.log('[YjxCouponModel.js] [getCustUserVideoDocDowUrl]  Error: ' + error);
            }
            connection.release();

            callback(error, result);
        });
    });
};





/*
* 更新用户自定义文字视频作品信息
*
* @author 毛凯
* @date 2018-08-07
*
* @param {Object} options
* @param {Number} options.device       设备标识
* @param {Number} options.opType       1 竖屏(单页/印品等)  2 视频  3 PPT, 4 自由创建, 5 文字转视频
* @param {Number} options.userId       用户唯一标识
* @param {Number} options.docId       统一的作品id
* @param {String} options.docJson    文档结构, 参见项目
* @param {Function} callback
*                       error
* */
YjxCustVideoUserDocModel.prototype.updateTextVideoDocInfo = function (options, callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxCustVideoUserDocModel.js [updateTextVideoDocInfo] getConErr: ' + getConErr);
            return callback(getConErr, null);
        }

        var sql =
            ' UPDATE yjx.yjx_cust_video_user_doc ycvud ' +
            ' SET ycvud.data = ' + options.docString + ', ' +
            '     ycvud.res_low = NULL, ' +
            '     ycvud.comb_res_low = NULL, ' +
            '     ycvud.res_mid = NULL, ' +
            '     ycvud.comb_res_mid = NULL, ' +
            '     ycvud.res_high = NULL, ' +
            '     ycvud.comb_res_high = NULL, ' +
            '     ycvud.dirty = 1, ' +
            '     ycvud.state = 0, ' +
            '     ycvud.modify_cnt = ycvud.modify_cnt+1, ' +
            '     ycvud.update_time = now(), ' +
            '     ycvud.update_by = ' + options.userId +
            ' WHERE ycvud.uni_doc_id = ' + options.docId +
            '   AND ycvud.user_id = ' + options.userId;
        console.log(sql);
        connection.query(sql, function (queryErr, result) {
            connection.release();
            if (queryErr) {
                console.log('YjxCustVideoUserDocModel.js [updateTextVideoDocInfo] queryErr' + queryErr);
                return callback(queryErr, null);
            }
            return callback(queryErr, result);
        });
    })
};



/*
* 获取文字视频的基础信息
*
* @author 毛凯
* @date 2018-08-07
*
* @param {Object} options
* @param {Number} options.doc_id       作品id
* @param {Number} options.user_id      用户Id
* @param {Function} callback
*                       error
* */
YjxCustVideoUserDocModel.prototype.getCustuserVideoInfo = function (options, callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxCustVideoUserDocModel.js [getCustuserVideoInfo] getConErr: ' + getConErr);
            return callback(getConErr, null);
        }

        var sql ="	SELECT	" +
                "	  *,	" +
                "	unix_timestamp(ycvud.create_time) AS createTimestamp	" +
                "	FROM	" +
                "	  yjx.yjx_cust_video_user_doc ycvud,	" +
                "	  itbour.tg_size ts	" +
                "	WHERE ycvud.doc_id = "+options.doc_id+"	" +
                "	AND ycvud.user_id = "+options.user_id+"	" +
                "	AND ycvud.del_flg = 0	" +
                "	AND ycvud.size_id = ts.size_id	" +
                "	AND ts.del_flg = 0	" ;
            console.log(sql);
        connection.query(sql, function (queryErr, result) {
            connection.release();
            if (queryErr) {
                console.log('YjxCustVideoUserDocModel.js [getCustuserVideoInfo] queryErr' + queryErr);
                return callback(queryErr, null);
            }
            return callback(queryErr, result);
        });
    })
};


/*
* 更新用户文字视频的信息
*
* @author 毛凯
* @date 2018-08-07
*
* @param {Number} options.docId     自由创建作品子表唯一标识
* @param {Number} options.userId
* @param {Number} options.modifyCnt
* @param {String} options.thumbs
* @param {String} options.update_by
* @param {String} options.res_low
* @param {String} options.res_mid
* @param {String} options.res_high
* @param {String} options.comb_res_low
* @param {String} options.comb_res_mid
* @param {String} options.comb_res_high
* @param {Number} options.userId
* @param {String} options.dirty
* @param {String} options.quality
* @param {String} options.state
* @param {String} options.resId
* @param {Function} callback
*                       error
* */
YjxCustVideoUserDocModel.prototype.updateUserTxtVideo = function (options, callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxCustVideoUserDocModel.js [getCustuserVideoInfo] getConErr: ' + getConErr);
            return callback(getConErr, null);
        }
        var sql ="	UPDATE	" +
                "	  yjx.yjx_cust_video_user_doc a1	" +
                "	  INNER JOIN	" +
                "	    (SELECT	" +
                "	      a2.modify_cnt	" +
                "	    FROM	" +
                "	      yjx.yjx_cust_video_user_doc a2	" +
                "	    WHERE a2.doc_id = "+options.docId+"	" +
                "	      AND a2.del_flg = 0	" +
                "	      AND a2.user_id = "+options.userId+") res	" +
                "	    ON "+options.modifyCnt+" = res.modify_cnt	" +
                "	    AND a1.doc_id = "+options.docId+"	" +
                "	    AND a1.del_flg = 0 	" +
                "	    AND a1.user_id ="+options.userId+" 	" +
                "	    SET a1.thumbs = '"+options.thumbs+"',	" +
                "	    a1.res_low = "+options.res_low+",	" +
                "	    a1.res_mid = "+options.res_mid+",	" +
                "	    a1.res_high = "+options.res_high+",	" +
                "	    a1.comb_res_low = "+options.comb_res_low+",	" +
                "	    a1.comb_res_mid = "+options.comb_res_mid+",	" +
                "	    a1.comb_res_high = "+options.comb_res_high+",	" +
                "	    a1.dirty = "+options.dirty+",	" +
                "	    a1.state = "+options.state+",	" ;
            if(options.quality === 1) {
                sql += "a1.res_low = "+options.resId+" ";
            }
            if(options.quality === 2) {
                sql += "a1.res_mid = "+options.resId+" ";
            }
            if(options.quality === 3) {
                sql += "a1.res_high = "+options.resId+" ";
            }
            console.log(sql);
        connection.query(sql, function (queryErr, result) {
            connection.release();
            if (queryErr) {
                console.log('YjxCustVideoUserDocModel.js [getCustuserVideoInfo] queryErr' + queryErr);
                return callback(queryErr, null);
            }
            return callback(queryErr, result);
        });
    })
};

module.exports = YjxCustVideoUserDocModel;